//
//  GHGoodMenuView.m
//  GodHorses
//
//  Created by Mac on 2017/11/18.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "GHGoodMenuView.h"
@interface GHGoodMenuView()
@property(nonatomic, strong) UIView                            *whiteView;
@property(nonatomic, strong) STAutoAddView                     *autoView;
@property(nonatomic, strong) STLabel                            *titleLable;
@property(nonatomic, strong) UIImageView                     *imageView;
@property(nonatomic, copy) void(^confimAction)(NSString* numText)                   ;
@end
@implementation GHGoodMenuView
- (void)dealloc{
    NSLog(@"GHGoodMenuView delloc");
}
- (void)setGoodModel:(GHGoodModel *)goodModel{
    _goodModel = goodModel;
    [_imageView sd_setImageWithURL:[NSURL URLWithString:self.goodModel.g_img] placeholderImage:GH_placeHoderImage];
    self.titleLable.text = [NSString stringWithFormat:@"￥%@%@",_goodModel.i_price,_goodModel.g_name];
}
#pragma mark --subView
- (void)configSubView{
    __weak typeof(self) weakSelf =  self;
    STButton * grayButton = [[STButton alloc] initWithFrame:UIScreenFrame
                                                     title:nil
                                                titleColor:nil
                                                 titleFont:0
                                              cornerRadius:0
                                           backgroundColor:[FlatBlack colorWithAlphaComponent:0.4]
                                           backgroundImage:nil
                                                     image:nil];
    [grayButton setClicAction:^(UIButton *sender) {
        [weakSelf showDismiss];
    }];
    [self addSubview:grayButton];
    
    UIView * whiteView = [[UIView alloc] initWithFrame:CGRectMake(0, UIScreenHeight, UIScreenWidth, 0)];
    whiteView.backgroundColor = [UIColor whiteColor];
    [self addSubview:whiteView];
    [whiteView st_showTopShadow];
    
    UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 120, 80)];
    [imageView sd_setImageWithURL:[NSURL URLWithString:self.goodModel.g_img] placeholderImage:GH_placeHoderImage];
    [whiteView addSubview:imageView];
    self.imageView = imageView;
    
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(imageView.right + 10, 15, UIScreenWidth -20 - imageView.st_right , 15)
                                                     text:[NSString stringWithFormat:@"￥%@%@",_goodModel.i_price,_goodModel.g_name]
                                                textColor:GH_firstTextColor
                                                     font:14
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    self.titleLable = titleLable;
    self.titleLable.attributedText = [self.titleLable.text st_convertAttributeStringWithKeyWord:[NSString stringWithFormat:@"￥%@",_goodModel.i_price] attributes:@{NSForegroundColorAttributeName:GH_redColor}];
    [whiteView addSubview:titleLable];
    
    STButton * colseButton = [[STButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)
                                                     title:nil
                                                titleColor:nil
                                                 titleFont:0
                                              cornerRadius:0
                                           backgroundColor:nil
                                           backgroundImage:nil
                                                     image:[UIImage imageNamed:@"sz_xha"]];
    colseButton.st_right = UIScreenWidth;
    [colseButton setClicAction:^(UIButton *sender) {
        [weakSelf showDismiss];
    }];
    [whiteView addSubview:colseButton];
    
    STLabel * numLable = [[STLabel alloc] initWithFrame:CGRectMake(10, imageView.st_bottom + 30, 200, 15)
                                                     text:@"购买数量"
                                                textColor:GH_secendTextColor
                                                     font:14
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    [whiteView addSubview:numLable];
    
    STAutoAddView * autoView =  [[STAutoAddView alloc] initWithFrame:CGRectMake(0, 0, 110, 30)];
    autoView.right = UIScreenWidth - 10;
    autoView.centerY = numLable.centerY;
    [whiteView addSubview:autoView];
    
    STButton * confimButton = [[STButton alloc] initWithFrame:CGRectMake(40, numLable.st_bottom + 20, UIScreenWidth - 80, 40)
                                                     title:@"确认"
                                                titleColor:GH_YellowBackGroundColor
                                                 titleFont:15
                                              cornerRadius:10
                                           backgroundColor:GH_BlackBackGroundColor
                                           backgroundImage:nil
                                                     image:nil];
    [confimButton setClicAction:^(UIButton *sender) {
        [weakSelf showConfimDismis];
    }];
    confimButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [whiteView addSubview:confimButton];
    
    whiteView.height = confimButton.st_bottom + 20;
    
    self.whiteView = whiteView;
    
    self.autoView = autoView;
}
- (void)showFromView:(UIView *)view handle:(void (^)(NSString *))handle{
    [view addSubview:self];
    if (handle) {
        self.confimAction = handle;
    }
    [UIView animateWithDuration:0.25 animations:^{
        self.whiteView.bottom = UIScreenHeight;
    }];
}
- (void)showDismiss{
    [UIView animateWithDuration:0.25 animations:^{
        self.whiteView.top = UIScreenHeight;
    }completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}
- (void)showConfimDismis{
    [UIView animateWithDuration:0.25 animations:^{
        self.whiteView.top = UIScreenHeight;
    }completion:^(BOOL finished) {
        [self removeFromSuperview];
        if (self.confimAction) {
            self.confimAction(self.autoView.currentNum);
        }
    }];
}
@end
